<template>
  <div class="app-container">

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['base:center-user:create']">新增</el-button>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="姓名" align="center" prop="nickname" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['base:center-user:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请选择用户" v-show="false" >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
          <el-input v-model="form.nickname" placeholder="请选择用户" disabled >
            <el-button slot="append" icon="el-icon-search" @click="openSelectUser = true"></el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <selectUser :openSelectUser.sync="openSelectUser" @selectUser="selectUser"></selectUser>
  </div>
</template>

<script>
import { createCenterUser, deleteCenterUser, getCenterUserPage } from "@/api/base/centerUser";
import SelectUser from '@/components/SelectUser/index.vue'

export default {
  name: "CenterUser",
  components: {
    SelectUser
  },
  props: {
    centerId: {
      required: true
    }
  },
  watch: {
    centerId: {
      immediate: true,
      handler(newVal) {
        this.activeCenterId = newVal
        this.getList()
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 工作中心人员绑定列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示选择用户
      openSelectUser: false,
      // 活跃工作中心
      activeCenterId: this.centerId,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        centerId: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        centerId: [{ required: true, message: "工作中心不能为空", trigger: "blur" }],
        userId: [{ required: true, message: "用户不能为空", trigger: "blur" }],
      }
    };
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.centerId = this.activeCenterId;
      // 执行查询
      getCenterUserPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        centerId: this.centerId,
        userId: undefined,
        nickname:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作中心人员配置";
    },
    /* 选择用户 */
    selectUser(data) {
      this.form.userId = data.id
      this.form.nickname = data.nickname
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 添加的提交
        createCenterUser(this.form).then(response => {
          this.$modal.msgSuccess("配置成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认移除姓名为"' + row.nickname + '"的数据项?').then(function() {
          return deleteCenterUser(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("移除成功");
        }).catch(() => {});
    },
  }
};
</script>
